using System;
using UnityEngine;



public class SACreatureAction : MonoBehaviour
{
	public delegate void onActionOverDelegate( int a );
	public delegate void onUpdateFrameDelegate( int a , float f , SATexture texture );

	[ HideInInspector ] public int actionID = GameDefine.INVALID_ID;
	[ HideInInspector ] public int actionType = GameDefine.INVALID_ID;
	[ HideInInspector ] public int direction = GameDefine.INVALID_ID;


	[ HideInInspector ] public SAAction.SAActionFrame actionFrame = null;
	[ HideInInspector ] public SAAction action = null;


	private int frameNow = 0;
	private float delay = 0.0f;
	private float delayTime = 0.0f;
	private bool loop = false;
	private bool isOver = true;
	private bool useSprite = true;

	public onActionOverDelegate actionOverDelegate = null;
	public onUpdateFrameDelegate updateFrameDelegate = null;

	public SACreatureAction()
	{

	}


	public void clearAction()
	{
		frameNow = 0;
		delay = 0.0f;
		delayTime = 0.0f;
		loop = false;
		isOver = true;

		actionFrame = null;
		action = null;

		actionID = GameDefine.INVALID_ID;
		actionType = GameDefine.INVALID_ID;
		direction = GameDefine.INVALID_ID;

		if ( !useSprite )
		{
			return;
		}

		SpriteRenderer renderer = (SpriteRenderer)gameObject.renderer;
		renderer.sprite = null;
	}


	public void setActionID( int id )
	{
		actionID = id;

		action = SAActionManager.instance.getAction( actionID );

		actionFrame = null;
	}


	public void setDirection( int d )
	{
		direction = d;
		actionFrame = action.getActionFrame( direction , actionType );

		if ( actionFrame == null )
		{
			frameNow = 0;
			isOver = true;
			return;
		}

		if ( !loop )
		{
			frameNow = 0;
			isOver = false;
		}

		updateSprite();
	}


	public int getSATextureID()
	{
		return actionFrame.frames[ frameNow ];
	}


	public void playAction( int a = (int)SAActionType.STAND , int d = (int)SAMapDirectionType.SOUTHWEST )
	{
		actionType = a;
		direction = d;

		actionFrame = action.getActionFrame( d , a );
		frameNow = 0;

		if ( actionFrame == null )
		{
			isOver = true;
			return;
		}

		delay = actionFrame.time / 1000.0f / (float)actionFrame.frameCount;

		if ( a == (int)SAActionType.ATTACK || a == (int)SAActionType.DEFENCE ||
		    a == (int)SAActionType.FALL || a == (int)SAActionType.HURT || a == (int)SAActionType.SIT || 
		    a == (int)SAActionType.THROW ) 
		{
			loop = false;
		}
		else
		{
			loop = true;
		}

		isOver = false;

		updateSprite();
	}

	public void setDelay( float d )
	{
		delay = d / (float)actionFrame.frameCount;
	}

	void updateSprite()
	{
		if ( !useSprite )
		{
			return;
		}

		if ( frameNow >= actionFrame.frames.Length ) 
		{
			Debug.LogError( "frameNow error" );
			return;
		}

		int id1 = actionFrame.frames[ frameNow ];

		if ( id1 > 0 )
		{
			SATexture saTexture = SATextureConfig.instance.getTexture( id1 );
			
			if ( saTexture == null )
			{
				return;
			}
			
			SpriteRenderer renderer = (SpriteRenderer)gameObject.renderer;
			renderer.sprite = saTexture.sprite;

			if ( updateFrameDelegate != null ) 
			{
				updateFrameDelegate( actionType , (float)frameNow / (float)actionFrame.frameCount , saTexture );	
			}
		}
	}


	void Update()
	{
		//if ( actionID == GameDefine.INVALID_ID )
		//{
			//SATexture texture = SATextureConfig.instance.getTexture( 110 );
			//texture.loadData();

			//setActionID( 101606 );
			//playAction( 0 , 0 );
			//loop = true;

			//SpriteRenderer renderer = (SpriteRenderer)gameObject.renderer;
			//renderer.sprite = texture.sprite;

			//actionID = 1;
		//}

		if ( isOver )
		{
			return;
		}

		delayTime += Time.deltaTime;

		if ( delayTime > delay )
		{
			delayTime = 0.0f;

			frameNow++;

			if ( loop )
			{
				if ( frameNow >= actionFrame.frameCount )
				{
					frameNow = 0;
				}

				updateSprite();
			}
			else
			{
				if ( frameNow >= actionFrame.frameCount )
				{
					frameNow = actionFrame.frameCount - 1;
					isOver = true;

					if ( actionOverDelegate != null ) 
					{
						actionOverDelegate( actionType );
					}
				}

				updateSprite();
			}
		}
	}

}







